package com.gaofans.nrpc.client.test;

import com.gaofans.nrpc.client.route.ServiceInstance;
import com.gaofans.nrpc.client.route.ServiceStorage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class MockServiceStore extends ServiceStorage {

    private final ConcurrentHashMap<String,List<ServiceInstance>> data = new ConcurrentHashMap<>();

    public synchronized void add(String serviceId,ServiceInstance instance){
        getInstances(serviceId).add(instance);

    }

    @Override
    public List<ServiceInstance> getInstances(String serviceId) {
        return data.computeIfAbsent(serviceId,s -> new ArrayList<>());
    }

    @Override
    public List<String> services() {
        return Collections.list(data.keys());
    }
}
